<?
$PageTitle=$msg["Page_Register"];
$hidelogon=1;

include "div_menu.php";


if ($_POST) {

  $user = add_user();

  if ($user) {

    $_SESSION['user'] = $user;

    echo '<br>';
    echo $msg["SuccessLogon"], ' ', $user["name"], '<br><br>';
    echo '<a href="news">', $msg["Continue"], '</a><br><br>';

  } else {
    make_form();
  };

} else {
  make_form();
};


//-----------------------------------------------------------------------------

  function make_form() 
  {
    global $msg;
  ?>

<!-- Register form -->

<form action="register" method="post">

  <fieldset class="fields1">

  <dl>
    <dt><label for="username"><?=$msg["UserName"];?></label></dt>
    <dd><input type="text" tabindex="1" size="25" name="username" id="username" value="<?=$_POST["username"];?>"/></dd>
  </dl>

  <dl>
    <dt><label for="email"><?=$msg["EMail"];?></label></dt>
    <dd><input type="text" tabindex="2" size="25" id="email" name="email" value="<?=$_POST["email"];?>"/></dd>
  </dl>

  <dl>
    <dt><label for="password"><?=$msg["Password"];?></label></dt>
    <dd><input type="password" tabindex="3" size="25" id="password" name="password"/></dd>
  </dl>

  <dl>
    <dt><label for="password1"><?=$msg["Password1"];?></label></dt>
    <dd><input type="password" tabindex="4" size="25" id="password1" name="password1"/></dd>
  </dl>

  <dl>
    <input type="submit" name="logon" tabindex="5" value="<?=$msg["Ready"];?>"/></dd>
  </dl>

  </fieldset>

</form>

  <?php
  };


//-----------------------------------------------------------------------------

  function add_user() 
  {
    global $msg;

    $n = 0;

    try 
    {
      db_call();

      $username = $_POST["username"];
      if ($username == "") {
        $errors[$n++] = $msg["NoUserName"];
      } else {
        if (db_check_user("name", $username) > 0) {
          $errors[$n++] = sprintf($msg["UserExists"], $username);
        }
      }

      $email = $_POST["email"];
      if ($email == "") {
        $errors[$n++] = $msg["NoEMail"];
      } else {
        if (db_check_user("email", $email) > 0) {
          $errors[$n++] = sprintf($msg["EMailExists"], $email);
        }
      }

      $password = $_POST["password"];
      if ($password == "") {
        $errors[$n++] = $msg["NoPassword"];
      } else {

        if ($password <> $_POST["password1"]) {
          $errors[$n++] = $msg["PasswordMismatch"];
        }

      }

      if ($n == 0) {
        db_add_user($username, $email, sha1($password));
        $user = db_logon($username, sha1($password));
      }

    } catch (Exception $e) 
    {
      $errors[$n++] = $e->getMessage();
    }

    if ($n <> 0) {
      print_errors($errors);
      return False;
    }

    return $user;
  };


?>